<!-- 折线图封装 -->

<template>
    <v-chart
        ref="myChart"
        class="echarts"
        :style="{height: height}"
        :option="option"
        autoresize
        @click="click"
    />
</template>

<script>
// eslint-disable-next-line no-unused-vars
import echarts from 'echarts'
import VChart from 'vue-echarts'
export default {
  name: 'EchartLine',
  components: {
    VChart
  },
  props: {
    dataSource: {
      type: Object,
      default: () => {
        return {}
      }
    },
    chartOption: {
      type: Object,
      default: () => {
        return {}
      }
    },
    height: {
      type: String,
      default: '300px'
    },
    unit: {
      type: String,
      default: ''
    }
  },
  computed: {
    option () {
      const option = {
        title: _.assign({}, this.defaultOption.title, this.chartOption.title || {}),
        tooltip: _.assign({}, this.defaultOption.tooltip, this.chartOption.tooltip || {}),
        grid: _.assign({}, this.defaultOption.grid, this.chartOption.grid || {}),
        legend: _.assign({}, this.defaultOption.legend, this.chartOption.legend || {}),
        xAxis: _.assign({}, this.defaultOption.xAxis, this.chartOption.xAxis, {data: this.dataSource.xAxis}),
        yAxis: _.assign({}, this.defaultOption.yAxis, this.chartOption.yAxis || {}),
        series: _.map(this.dataSource.yAxis, dataItem => {
          return {
            type: 'line',
            symbol: dataItem.symbol || 'circle',
            smooth: dataItem.smooth !== false,
            symbolSize: dataItem.symbolSize || 8,
            showSymbol: dataItem.showSymbol || (dataItem.data.length === 1),
            name: dataItem.name,
            data: dataItem.data,
            itemStyle: {
              color: dataItem.color,
              borderColor: 'rgba(255,255,255,0.8)',
              borderWidth: 2
            }
          }
        })
      }
      return option
    }
  },
  data () {
    return {
      defaultOption: {
        title: {
          x: '5%'
        },
        tooltip: {
          trigger: 'axis',
          textStyle: {
            color: '#fff'
          },
          backgroundColor: 'rgba(51,51,51,0.80)',
          padding: [14, 20]
        },
        grid: {
          top: '15%',
          left: '24',
          right: '24',
          bottom: '60',
          containLabel: true
        },
        //如果是堆叠折线图，这里可以放出来
        // legend: {
        //   left: 'center',
        //   bottom: '26',
        //   itemGap: 25,
        //   itemWidth: 8,
        //   itemHeight: 8,
        //   show: true,
        //   icon: 'circle',
        //   textStyle: {
        //     color: () => {
        //       return _.map(this.dataSource.yAxis, item => {
        //         return item.color
        //       })
        //     }
        //   }
        // },
        xAxis: {
          axisLabel: {
            margin: 12,
            textStyle: {
              color: '#666'
            }
          },
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#E8E8E8'
            }
          },
          splitLine: {
            show: false
          }
        },
        yAxis: {
          minInterval: 1,
          splitNumber: 5,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#E8E8E8'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#E8E8E8',
              opacity: 0.5,
              type: 'dotted'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#666'
            }
          }
        }
      }
    }
  },
  methods: {
    click (e) {
      this.$emit('click', e)
    }
  }
}
</script>
<style  scoped>
.echarts{
    width: 100%;
    height: 100%;
}
</style>
